home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 July / july_2000.iso / Site Building / port scanner / portScanner.java < prev    next >
Encoding:
Java Source  |  2000-06-07  |  10.1 KB  |  305 lines

  1. /*
  2.  * Java Port Scanner
  3.  *
  4.  * Copyright 2000 Matteo Baccan <mbaccan@planetisa.com>
  5.  * www - http://www.infomedia.it/artic/Baccan
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
  20.  * their web site at http://www.gnu.org/).
  21.  *
  22.  */
  23.  
  24. import java.net.*;
  25.  
  26. public class portScanner {
  27.  
  28.    /* Public interface ******************************************************/
  29.    static public void main( String argv[] ) {
  30.       String cIPFrom;
  31.       String cPortFrom;
  32.       String cWebTo;
  33.       String cPortTo;
  34.  
  35.       System.out.println( "+---------------------------------------------------------------------------+" );
  36.       System.out.println( "| TCP/IP Port Scanner                              Version 0.11  04-06-2000 |" );
  37.       System.out.println( "| (C) 2000 by Matteo Baccan            http://www.infomedia.it/artic/Baccan |" );
  38.       System.out.println( "+---------------------------------------------------------------------------+" );
  39.  
  40.       try {
  41.          if( argv.length==0 ){
  42.             System.out.println( "Usage: java portScanner [-ip <ip>] [options] " );
  43.             System.out.println( " "                      );
  44.             System.out.println( "<options>"              );
  45.             System.out.println( "   -fromip <ip>"        );
  46.             System.out.println( "   -toip <ip>"          );
  47.             System.out.println( "   -fromport <nPort>"   );
  48.             System.out.println( "   -toport <nPort>"     );
  49.             System.out.println( "   -portfile <cFile>"   );
  50.             System.out.println( "   -thread <nThread>"   );
  51.             System.out.println( "   -log"                );
  52.             System.out.println( "   -timeout <nTimeout>" );
  53.          } else {
  54.             portScanner scanner = new portScanner();
  55.             for( int nParam=0; nParam<argv.length; nParam++ ){
  56.                       if( argv[nParam].equalsIgnoreCase("-ip") ){
  57.                  if( nParam+1<argv.length ){
  58.                     scanner.setIPFrom( argv[nParam+1] );
  59.                     scanner.setIPTo( argv[nParam+1] );
  60.                  }
  61.                } else if( argv[nParam].equalsIgnoreCase("-fromip") ){
  62.                  if( nParam+1<argv.length ){
  63.                     scanner.setIPFrom( argv[nParam+1] );
  64.                  }
  65.                } else if( argv[nParam].equalsIgnoreCase("-toip") ){
  66.                  if( nParam+1<argv.length ){
  67.                     scanner.setIPTo( argv[nParam+1] );
  68.                  }
  69.                } else if( argv[nParam].equalsIgnoreCase("-fromport") ){
  70.                  if( nParam+1<argv.length ){
  71.                     scanner.setPortFrom( Integer.parseInt( argv[nParam+1] ) );
  72.                  }
  73.                } else if( argv[nParam].equalsIgnoreCase("-toport") ){
  74.                  if( nParam+1<argv.length ){
  75.                     scanner.setPortTo( Integer.parseInt( argv[nParam+1] ) );
  76.                  }
  77.                } else if( argv[nParam].equalsIgnoreCase("-portfile") ){
  78.                  if( nParam+1<argv.length ){
  79.                     scanner.setPortFile( argv[nParam+1] );
  80.                  }
  81.                } else if( argv[nParam].equalsIgnoreCase("-thread") ){
  82.                  if( nParam+1<argv.length ){
  83.                     scanner.setThread( Integer.parseInt( argv[nParam+1] ) );
  84.                  }
  85.                } else if( argv[nParam].equalsIgnoreCase("-log") ){
  86.                  scanner.setLog( true );
  87.                } else if( argv[nParam].equalsIgnoreCase("-timeout") ){
  88.                  if( nParam+1<argv.length ){
  89.                     scanner.setTimeout( Integer.parseInt( argv[nParam+1] ) );
  90.                  }
  91.                }
  92.             }
  93.  
  94.             scanner.log("Parameter");
  95.             scanner.log("---------");
  96.             scanner.log("IP   from [" +scanner.getIPFrom() +"]");
  97.             scanner.log("IP   to   [" +scanner.getIPTo()   +"]");
  98.             if( scanner.usePortFile() )
  99.                scanner.log("Port file [" +scanner.getPortFile() +"]");
  100.             else {
  101.                scanner.log("Port from [" +scanner.getPortFrom() +"]");
  102.                scanner.log("Port to   [" +scanner.getPortTo()   +"]");
  103.             }
  104.             scanner.log("Thread    [" +scanner.getThread()   +"]");
  105.             scanner.log("Log       [" +scanner.getLog()      +"]");
  106.             scanner.log("Timeout   [" +scanner.getTimeout()  +"]");
  107.             scanner.log("" );
  108.             scanner.run();
  109.          }
  110.  
  111.       } catch(Exception e) {
  112.          System.out.println( e.toString() );
  113.       }
  114.    }
  115.    /*************************************************************************/
  116.  
  117.    public portScanner(){}
  118.  
  119.    private byte[] cIPFrom = {127,0,0,1};
  120.    public void setIPFrom( String cIP ){
  121.       try {
  122.          cIPFrom = InetAddress.getByName( cIP ).getAddress();
  123.       } catch ( Throwable e ){
  124.          System.out.println( e.toString() );
  125.       }
  126.    }
  127.    public String getIPFrom(){
  128.       return ipToString( cIPFrom );
  129.    }
  130.  
  131.    private String ipToString( byte []cIP ){
  132.       return (cIP[0]&0xFF)+ "."+
  133.              (cIP[1]&0xFF)+ "."+
  134.              (cIP[2]&0xFF)+ "."+
  135.              (cIP[3]&0xFF);
  136.    }
  137.  
  138.    private byte[] cIPTo = {127,0,0,1};
  139.    public void setIPTo( String cIP ){
  140.       try {
  141.          cIPTo = InetAddress.getByName( cIP ).getAddress();
  142.       } catch ( Throwable e ){
  143.          System.out.println( e.toString() );
  144.       }
  145.    }
  146.    public String getIPTo(){
  147.       return ipToString( cIPTo );
  148.    }
  149.  
  150.    private int nPortFrom = 1;
  151.    public void setPortFrom( int nPort ){
  152.       nPortFrom = nPort;
  153.    }
  154.    public int getPortFrom(){
  155.       return nPortFrom;
  156.    }
  157.  
  158.    private int nPortTo = 1024;
  159.    public void setPortTo( int nPort ){
  160.       nPortTo = nPort;
  161.    }
  162.    public int getPortTo(){
  163.       return nPortTo;
  164.    }
  165.  
  166.  
  167.    private String cPortFile = "";
  168.    public void setPortFile( String cFile ){
  169.       cPortFile = cFile;
  170.    }
  171.    public String getPortFile(){
  172.       return cPortFile;
  173.    }
  174.  
  175.    public void setThread( int nThread ){
  176.       nMaxThread = nThread;
  177.    }
  178.    public int getThread(){
  179.       return nMaxThread;
  180.    }
  181.  
  182.    private boolean bLog=false;
  183.    public void setLog( boolean bLog ){
  184.       this.bLog = bLog;
  185.    }
  186.    public boolean getLog(){
  187.       return bLog;
  188.    }
  189.  
  190.    public void log( int nMsg ){
  191.       log( new Integer( nMsg ).toString() );
  192.    }
  193.    public void log( String cMsg ){
  194.       System.out.println( cMsg );
  195.    }
  196.  
  197.    private int nTimeout = 2000;
  198.    public void setTimeout( int nTime ){
  199.       nTimeout = nTime;
  200.    }
  201.    public int getTimeout(){
  202.       return nTimeout;
  203.    }
  204.  
  205.    public boolean usePortFile(){
  206.       return (cPortFile.length()>0);
  207.    }
  208.  
  209.    public void run() {
  210.       port portToScan = new port();
  211.       if( usePortFile() ) {
  212.          portToScan.setFile( cPortFile );
  213.       } else {
  214.          portToScan.setRange( nPortFrom, nPortTo );
  215.       }
  216.  
  217.       long nMils = System.currentTimeMillis();
  218.       Thread ip;
  219.       while( true ){
  220.  
  221.          if( cIPFrom[0]>=cIPTo[0] &&
  222.              cIPFrom[1]>=cIPTo[1] &&
  223.              cIPFrom[2]>=cIPTo[2] &&
  224.              cIPFrom[3]>=cIPTo[3] ){
  225.             if( !(cIPFrom[0]==cIPTo[0] &&
  226.                   cIPFrom[1]==cIPTo[1] &&
  227.                   cIPFrom[2]==cIPTo[2] &&
  228.                   cIPFrom[3]==cIPTo[3] ) ){
  229.                break; // End of thread
  230.             }
  231.          }
  232.  
  233.          for( int nLocalPort=0; nLocalPort< portToScan.length(); nLocalPort++ ){
  234.             if( !isMaxThread() ){
  235.                threadAdd();
  236.                ip = new scanSingleIP( this,
  237.                                       ipToString(cIPFrom),
  238.                                       portToScan.getPort(nLocalPort),
  239.                                       portToScan.getDesc(nLocalPort),
  240.                                       bLog,
  241.                                       nTimeout );
  242.                ip.start();
  243.             } else {
  244.                try {
  245.                   Thread.sleep( 500 );
  246.                } catch ( Throwable e ){}
  247.             }
  248.          }
  249.  
  250.          if( cIPFrom[3]<255 ){
  251.             cIPFrom[3]++;
  252.          } else {
  253.             cIPFrom[3]=0;
  254.             if( cIPFrom[2]<255 ){
  255.                cIPFrom[2]++;
  256.             } else {
  257.                cIPFrom[2]=0;
  258.                if( cIPFrom[1]<255 ){
  259.                   cIPFrom[1]++;
  260.                } else {
  261.                   cIPFrom[1]=0;
  262.                   if( cIPFrom[0]<255 ){
  263.                      cIPFrom[0]++;
  264.                   } else {
  265.                      break; // No more address
  266.                   }
  267.                }
  268.             }
  269.          }
  270.       }
  271.  
  272.       while( isActiveThread() ){
  273.          try {
  274.             Thread.sleep(500 );
  275.          } catch ( Throwable e ){}
  276.       }
  277.  
  278.       nMils = (System.currentTimeMillis()-nMils)/1000;
  279.       log( "Seconds : " +nMils );
  280.    }
  281.  
  282.    //------------------------------------------------------------------------//
  283.    // Thread Manager                                                         //
  284.    //------------------------------------------------------------------------//
  285.    private int nThread    = 0;
  286.    private int nMaxThread = 50;
  287.    private int nPortTotal = 0;
  288.    synchronized boolean isMaxThread() {
  289.       return (nThread>=nMaxThread);
  290.  
  291.    }
  292.    synchronized boolean isActiveThread() {
  293.       return (nThread>0);
  294.  
  295.    }
  296.    public synchronized void threadAdd() {
  297.       nPortTotal++;
  298.       nThread++;
  299.    }
  300.    public synchronized void threadDel() {
  301.       nThread--;
  302.    }
  303.    //--------------------------------------------------------------------------
  304. }
  305.